package com.xitang.customer.controller;

import com.xitang.common.bean.BizProduct;
import com.xitang.common.bean.BizProductDetail;
import com.xitang.common.dto.PageQueryRsp;
import com.xitang.common.dto.ProductQueryReq;
import com.xitang.common.dto.ResultVO;
import com.xitang.customer.service.ICustomerProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "产品接口列表-客户")
@RestController
public class CustomerProductController extends BaseCustomerController {
    @Autowired
    private ICustomerProductService productService;

    @Value("${qiniu.domain}")
    private String domain;

    @ApiOperation(value = "获取产品列表信息", notes = "获取产品列表信息")
    @GetMapping("/product")
    public ResultVO<List<BizProduct>> productList(@RequestParam(name = "page") Integer page,
                                                  @RequestParam(name = "limit") Integer limit,
                                                  @RequestParam(name = "categoryCode", required = false) String categoryCode,
                                                  @RequestParam(name = "productCode", required = false) String productCode,
                                                  @RequestParam(name = "productName", required = false) String productName,
                                                  @RequestParam(name = "productColor", required = false) String productColor,
                                                  @RequestParam(name = "productPrice", required = false) Float productPrice,
                                                  @RequestParam(name = "productTexture", required = false) String productTexture,
                                                  @RequestParam(name = "productStyle", required = false) String productStyle,
                                                  @RequestParam(name = "productDesc", required = false) String productDesc) {
        ProductQueryReq queryReq = ProductQueryReq.builder()
                .pageNo(page)
                .pageSize(limit)
                .categoryCode(categoryCode)
                .productCode(productCode)
                .productName(productName)
                .productColor(productColor)
                .productPrice(productPrice)
                .productTexture(productTexture)
                .productStyle(productStyle)
                .productDesc(productDesc)
                .build();
        PageQueryRsp<BizProduct> pageQueryRsp = productService.getProductList(queryReq);
        Map<String, Object> headers = new HashMap<>();
        headers.put("domain", domain);
        headers.put("totalCount", pageQueryRsp.getTotal());
        return ResultVO.success(pageQueryRsp.getRecords(), headers);
    }

    @ApiOperation(value = "获取产品详细信息", notes = "根据产品编码获取产品详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "productCode", value = "产品编码", dataType = "String")
    })
    @GetMapping("/product/{productCode}")
    public ResultVO<BizProductDetail> productDetail(@PathVariable("productCode") String productCode) {
        Map<String, Object> headers = new HashMap<>();
        headers.put("domain", domain);
        return ResultVO.success(productService.getProductDetail(productCode), headers);
    }
}
